# Copyright (C) 2011 David Sugar
#
# Create automatic checkouts that add directories to build trees like
# add_subdirectory, but with repository references to gather the
# directory from if the requested directory is not found.  Also a
# generic checkout macro is provided.

macro(git_checkout dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        if("${repo}" STREQUAL "SVN")
            set(upstream "${ARGV3}")
            execute_process(COMMAND git svn clone -A
                "${CMAKE_SOURCE_DIR}/Authors" "${ARGV2}" "${dir}"
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
        else()
            if("${ARGV2}" STREQUAL "BRANCH")
                set(upstream "${ARGV4}")
                execute_process(COMMAND git clone -b "${ARGV3}" "${repo}" "${dir}"
                    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
            else()
                set(upstream "${ARGV2}")
                execute_process(COMMAND git clone "${repo}" "${dir}"
                    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
            endif()
        endif()
        if(NOT "${upstream}" STREQUAL "")
            execute_process(COMMAND git remote add upstream "${upstream}"
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        endif()
    endif()
endmacro(git_checkout dir repo)

macro(git_subdirectory dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        if("${repo}" STREQUAL "SVN")
            set(upstream "${ARGV3}")
            execute_process(COMMAND git svn clone -A
                "${CMAKE_SOURCE_DIR}/Authors" "${ARGV2}" "${dir}"
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
        else()
            set(upstream "${ARGV2}")
            execute_process(COMMAND git clone "${repo}" "${dir}"
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
        endif()
        if(NOT "${upstream}" STREQUAL "")
            execute_process(COMMAND git remote add upstream "${upstream}"
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        endif()
    endif()
    add_subdirectory("${dir}")
endmacro(git_subdirectory dir repo)

macro(svn_checkout dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        execute_process(COMMAND svn checkout ${repo} ${dir}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
endmacro(svn_checkout dir repo)

macro(svn_subdirectory dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        execute_process(COMMAND svn checkout ${repo} ${dir}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
    add_subdirectory("${dir}")
endmacro(svn_subdirectory dir repo)

macro(bzr_checkout dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        execute_process(COMMAND bzr checkout ${repo} ${dir}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
endmacro(bzr_checkout dir repo)

macro(bzr_subdirectory dir repo)
    if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
        execute_process(COMMAND bzr checkout ${repo} ${dir}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
    add_subdirectory("${dir}")
endmacro(bzr_subdirectory dir repo)

